package service.impl;

import dao.BookingDao;
import entity.Booking;
import org.apache.ibatis.session.SqlSession;
import service.BookingService;
import utils.MybatisUtil;

import java.util.List;

public class BookingServiceImpl implements BookingService {
    @Override
    public List<Booking> selectList(int pageIndex, int pageSize,String userName,int status) {
        SqlSession sqlSession = null;
        int startIndex = (pageIndex - 1) * pageSize;
        List<Booking> list = null;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            list = sqlSession.getMapper(BookingDao.class).selectList(startIndex,pageSize,userName,status);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    @Override
    public int count(int pageSize,String userName,int status) {
        SqlSession sqlSession = null;
        int count = 0;
        int pageCount = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            count = sqlSession.getMapper(BookingDao.class).count(userName,status);
//            System.out.println(count);
            pageCount = count % pageSize > 0 ? count / pageSize + 1 : count / pageSize;
//            System.out.println(pageCount);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }

        return pageCount;
    }

    @Override
    public int insert(Booking booking) {
        SqlSession sqlSession = null;
        int result = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingDao.class).insert(booking);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public int update(int id) {
        SqlSession sqlSession = null;
        int result = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingDao.class).update(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public int delete(int id) {
        SqlSession sqlSession = null;
        int result = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingDao.class).delete(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public int selectBooking(String cardId) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            count = sqlSession.getMapper(BookingDao.class).selectBooking(cardId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }
}
